package pl.edu.agh.portal.application.aspects;

import java.io.IOException;

import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import pl.edu.agh.portal.application.portlets.ThesisGenericPortlet;
import pl.edu.agh.portal.application.utils.PortletsUtils;
import pl.edu.agh.thesis.webservices.employee.client.EmployeeUtils;


public aspect SessionCheckingAspect {

	private pointcut sessionUpdate() : execution(void ThesisGenericPortlet.doView(RenderRequest, RenderResponse)) ;
			
	void around() throws PortletException, IOException : sessionUpdate() {
		int session = EmployeeUtils.validate(PortletsUtils.temp_session);
		if (session == 0) {
			ThesisGenericPortlet portlet = (ThesisGenericPortlet)thisJoinPoint.getThis();
			PortletRequestDispatcher view = portlet.getNormalView();
			view = portlet.getPortletContext().getRequestDispatcher(PortletsUtils.SESSION_EXPIRED_PAGE);
			portlet.setNormalView(view);
			
			Object[] args = thisJoinPoint.getArgs();
			
			portlet.emergencyDoView((RenderRequest)args[0], (RenderResponse)args[1]);			
		} else {
			proceed();
		}
	}	
				
}
